package com.ymatou.iapi.productmanager.compare.publish;

import java.io.File;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ymatou.iapi.productmanager.compare.oldapi.OldGetProductCatalogInfosCall;
import com.ymatou.iapi.productmanager.compare.publish.bean.productAndCatalogInfo.ProductAndCatalogInfoBean;
import com.ymatou.iapi.productmanager.compare.publish.bean.productCatalogInfo.ProductcataloginfoReponse;
import com.ymatou.iapi.sellerproduct.service.AddNewSellerProductCall;
import com.ymatou.iapi.sellerproduct.service.GetProductAndCatalogInfoCall;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.compare.AssertHelper;
import com.ymttest.utils.compare.LogPathConf;
import com.ymttest.utils.compare.LoggerHelper;
import com.ymttest.utils.compare.LoggerInterface;

public class AddProductCompare {
	private final static Integer sellerId = 20336378;
	private final static String sellerName = "fyftest1";
	private final static LogPathConf logPathConf = new LogPathConf(false);
	private final static LoggerInterface logger = new LoggerHelper(logPathConf);
	
	public static void compareAddProduct(String fileName,JSONObject request){

			
			initAndUpdateRequest(request);
			
			AddNewSellerProductCall addCall = new AddNewSellerProductCall();
			//call old
			String old_productId="";
			String old_msg = "";
			try {
				JSONObject ret = addCall.callAndGetReturnData(request);
				old_msg = ret.getString("Msg");
				old_productId = ret.getJSONObject("Data").getString("ProductID");
			} catch (JSONException e) {
				logger.failure(fileName+",old productid is null,"+old_msg,e);
				return;
			}
			
			//call new
			String new_productId="";
			String new_msg="";
			try {
				request.put("TurnTo", "java");
				JSONObject ret = addCall.callAndGetReturnData(request);
				new_msg = ret.getString("Msg");
				new_productId = ret.getJSONObject("Data").getString("ProductID");
				
			} catch (JSONException e) {
				logger.failure(fileName+",new productid is null,"+new_msg,e);
				return;
			}
			
			
			
			
			
			if (old_productId==null||old_productId.equals("")){
				logger.failure(fileName+", old can't get productId");
				return;
			}
			
			
			if (new_productId==null || new_productId.equals("")){
				logger.failure(fileName+", new can't get productId");
				return;
			}
				
			
			checkProductCatalogInfos(fileName,old_productId,new_productId);
			checkProductAndCatalogInfo(fileName,old_productId,new_productId);
			
			
		
		
	}
	
	
	public static void checkProductCatalogInfos(String fileName,String oldProd,String newProd){
		OldGetProductCatalogInfosCall oldCatalogInfoCall = new OldGetProductCatalogInfosCall();
		try{
			String desc = "File Name"+fileName+",ApiName: " + oldCatalogInfoCall.getApiName() + ", old product:" + oldProd+", new product:"+newProd;
			
			JSONObject oldRet = oldCatalogInfoCall.call(sellerId, oldProd);
			JSONObject newRet = oldCatalogInfoCall.call(sellerId, newProd);
			
			
			if (oldRet.isNull("Data")&&newRet.isNull("Data")){
				logger.verifyEquals(true, true, desc+",new return and old return are all null");
				return;
			}else if (!oldRet.isNull("Data")&&newRet.isNull("Data")){
				logger.failure(desc+",new return is null and old is not null");
				return;
			}else if (oldRet.isNull("Data")&&!newRet.isNull("Data")){
				logger.failure(desc+",old return is null and new is not null");
				return;
			}
			
			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();
			JSONObject oldData = oldRet.getJSONObject("Data");
			JSONObject newData = newRet.getJSONObject("Data");
			ProductcataloginfoReponse oldBean = gson.fromJson(oldData.toString(), ProductcataloginfoReponse.class);
			ProductcataloginfoReponse newBean = gson.fromJson(newData.toString(), ProductcataloginfoReponse.class);
			AssertHelper.assertResultEqual(oldBean,newBean,desc);
		}catch(Exception e){
			logger.failure(fileName+",checkProductCatalogInfos", e);
		}
	}
	
	
	
	private static void checkProductAndCatalogInfo(String fileName,String oldProd,String newProd) {
		GetProductAndCatalogInfoCall oldGetProductAndCatalogInfoCall = new com.ymatou.iapi.sellerproduct.service.GetProductAndCatalogInfoCall();
		try{
			String desc = "File Name"+fileName+",ApiName: " + oldGetProductAndCatalogInfoCall.getApiName() + ", old product:" + oldProd+", new product:"+newProd;
			
			JSONObject oldRet = oldGetProductAndCatalogInfoCall.callAndGetReturnData(sellerId, oldProd);
			JSONObject newRet = oldGetProductAndCatalogInfoCall.callAndGetReturnData(sellerId, newProd);
			
			
			if (oldRet.isNull("Data")&&newRet.isNull("Data")){
				logger.verifyEquals(true, true, desc+",new return and old return are all null");
				return;
			}else if (!oldRet.isNull("Data")&&newRet.isNull("Data")){
				logger.failure(desc+",new return is null and old is not null");
				return;
			}else if (oldRet.isNull("Data")&&!newRet.isNull("Data")){
				logger.failure(desc+",old return is null and new is not null");
				return;
			}
			
			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();
			JSONObject oldData = oldRet.getJSONObject("Data");
			JSONObject newData = newRet.getJSONObject("Data");
			ProductAndCatalogInfoBean oldBean = gson.fromJson(oldData.toString(), ProductAndCatalogInfoBean.class);
			ProductAndCatalogInfoBean newBean = gson.fromJson(newData.toString(), ProductAndCatalogInfoBean.class);
			logger.verifyEquals(oldBean.getProductAndCatalogInfoOnSellerProduct().getCatalogInfos().size(), 
					newBean.getProductAndCatalogInfoOnSellerProduct().getCatalogInfos().size(), desc);
			AssertHelper.assertResultEqual(oldBean,newBean,desc);
		}catch(Exception e){
			logger.failure(fileName+",checkProductAndCatalogInfo",e);
		}
	}
	
	
	
	
	
	
	public static void initAndUpdateRequest(JSONObject request){
		//set logger
		AssertHelper.setLoggerInterface(logger);
		//update sellerId and stock;
		try {
			request.put("UserID", sellerId);
			request.getJSONObject("Product").remove("VideoModel");
			request.getJSONObject("Product").put("LiveIdList", new JSONArray());
			request.put("LoginID", sellerName);
		JSONArray catalogAry = request.getJSONObject("Product").getJSONArray("Catalogs");
		for (int i=0;i<catalogAry.length();i++){
			catalogAry.getJSONObject(i).put("Num", (i+1)*10);
			catalogAry.getJSONObject(i).put("Price", 20000);
		}
		
		JsonBeanHelper.updateJsonBean(request, MapUtil.hashMap("Product->iBrandId",13758,"Product->sBrandName","Air Jordan",
																"Product->iCategoryId",1420,"Product->iThirdCategoryId",1460));
		}catch (JSONException e) {
			logger.failure("initAndUpdateRequest", e);
		}
	}
	
	
	
	public static void startCompareAddProduct(){
		String folder = "json";
		String fullPath = JsonBeanHelper.JsonBeanFileFolder + File.separator + folder;
		File f = new File(fullPath);
		File file[] = f.listFiles();
		for(int i=0;i<file.length;i++){
			String fileName = file[i].getName();
			System.out.println(fileName);
			try{
				JSONObject request =  JsonBeanHelper.getJsonBean(folder+File.separator+fileName);
				compareAddProduct(fileName,request);
			}catch(Exception e){
				logger.failure(fileName,e);
			}
			
			
		}
		
	}
	
	
	
	public static void main(String arg[]) throws Exception{
		startCompareAddProduct();
		
	}
	
	
	
}
